var pageNum = 10;

function getSkillInfo(id){
	$.cookie('xkh_lock','open'); 
	window.location.href="../home/getSkillInfo.html?id="+id;
}



$(function() {
	
	var itemIndex = 0;
	var dataIndex = 0;
	var sid = 0;
	var page = 1;
    var tab1LoadEnd = false;
    var tab2LoadEnd = false;
    var tab3LoadEnd = false;
    var tab4LoadEnd = false;
    var tab5LoadEnd = false;
    var val1=""
    var las = labelList;
    var citys = proList;
    var dataname ="";
    //定义公共的引用不到，在这个js中进行数据赋值
    var classSon_image = "x-oss-process=image/resize,m_fill,w_380,h_194,limit_0/auto-orient,0/quality,q_100";
    var classSon_headImage = "x-oss-process=image/resize,m_fill,w_52,h_52,limit_0/auto-orient,0/quality,q_70"
   
    //点击搜索图标
   /* $(".class_list_a").click(function(){
    	$(".form").css("display","none");
    	$(".class_title").css("display","none");
    	$(".class_choose").css("display","none");
    	$(".lists").css("display","none");
    	$(".dropload-down").css("display","none");
    	$(".ctt_hot").css("display","block");
    	$(".ctt_cancel").css("display","block");
    	$(".list_content").css("display","block")
    })*/
    $(".ctt_cancel").click(function() {
		$(".hot_inp").val("")
	})
	//点击选项卡切换
	$(".class_title1 li").click(function() {
		
			$(this).addClass("title_active").siblings().removeClass("title_active");

			var index = $(this).index();
			$(".class_title1 span").removeClass("title_color").eq(index).addClass("title_color");
			
		})
		//ajax请求这一部分
	$(".class_title1 li").click(function() {
		
		
		jQuery("#maxPrice").val("");
 		jQuery("#minPrice").val("");
 		jQuery("#cityName").val("");
 		jQuery("#schoolLabel").val(""); 
 		jQuery("#serviceType").val(""); 
		
		var $this = $(this);
        itemIndex = $this.index();
        dataIndex = this.dataset.name;
      //  page = this.value;
        sid = this.id;
        
        var pn = parseInt(document.getElementById(sid).getAttribute("value"));
      //  alert("=============page:"+pn);
       
        jQuery("#xkh_page_num").val(pn); //记录页数
        jQuery("#xkh_class_id").val(sid); //记录二级分类的id
        
        
        $this.addClass('cur').siblings('.item').removeClass('cur');
        $('.lists').eq(itemIndex).show().siblings('.lists').hide();
         // 如果选中菜单一
        if(itemIndex == dataIndex){
        	var $this = $(this);
            itemIndex = $this.index();
            dataIndex = this.dataset.name;
          //  page = this.value;
            sid = this.id;
            $this.addClass('cur').siblings('.item').removeClass('cur');
            $('.lists').eq(itemIndex).show().siblings('.lists').hide();
            // 如果数据没有加载完
            if(!tab1LoadEnd){
                // 解锁
                dropload.unlock();
                dropload.noData(false);
            }else{
                // 锁定
//              dropload.lock('down');
//              dropload.noData();
            	dropload.unlock();
                dropload.noData(false);
        }
        // 重置
       dropload.resetload();
      }
    });
  
    
//    设置弹出框始终在屏幕中央
    var width=$(".menu").width();
    var height=$(".menu").height();
    var left=(document.documentElement.clientWidth-width)/2;
    var top=(document.documentElement.clientHeight-height)/2;
    $(".menu").css({"left":left,"top":top});
    $(".class_sort li").click(function(){
    	//alert("bbbb")
		$(this).addClass("class_active").siblings().removeClass("class_active");
		var index=$(this).index();
		dataname = this.dataset.name
		$(".class_sort span").removeClass("icon-gou").eq(index).addClass("icon-gou");
		var data_n = $("#orderp"+sid).val();
		var gao=$("body").height();
		console.log(gao);
		if(dataname == data_n){
			//$(".menu").css("display","block")
//			$(".sort_mask").css({"height":gao+"px","position":'absolute',"width":"100vw","background":"#000","z-index": 100,"opacity": 0.2})
//		    $(".menu_child2").click(function(){
//		    	$(".sort_mask").css("height",0);
//		    	$(".menu").css("display","none");
//		    });
			
			$(".class_sortContent").addClass("show");
			
		}else{
			jQuery("#orderp"+sid).val(dataname);
			$("#"+sid).val(2);
			 dropload.unlock();
             dropload.noData(false);
            var fid2 =  $("#fid2").val();
			serchList(sid,dataname,fid2)
			$(".class_sortContent").addClass("show");
		}
//		延迟一秒以后关闭默认排序 ----不能这样设置这样设置dropload不起作用
//		setTimeout(function(){
//			$(".class_sortContent").addClass("show");
//		},1000)

	});
    // dropload
    var dropload = $('.content').dropload({
        scrollArea : window,
        loadDownFn : function(me){
            // 加载菜单一的数据
            if(itemIndex == dataIndex){
            	var orderp = jQuery("#orderp"+sid).val();
            	
            	var fid2 =  $("#fid2").val();
            	page=parseInt(document.getElementById(sid).getAttribute("value"));
            	var max = jQuery("#maxPrice").val();
        		var min = jQuery("#minPrice").val();
        		var cname = jQuery("#cityName").val();
        		var schoolLabel = jQuery("#schoolLabel").val(); 
        		var serviceType = jQuery("#serviceType").val(); 
        		
        		var lock = $.cookie('xkh_lock');
        		/*if(lock == 'open'){
                	var xkh_tab_id = $.cookie('xkh_tab_id'); 
            		var xkh_page_num = $.cookie('xkh_page_num'); 
            		page = 1;
            		pageNum = xkh_page_num*pageNum;
            	}*/
          //  	alert("pageNum="+pageNum+"**************"+"page="+page);
        		var xkh_page_num = jQuery("#xkh_page_num").val();
        		var xkh_class_id = jQuery("#xkh_class_id").val();
//        		var xkh_page_num = $.cookie('xkh_page_num'); 
        		//alert("xkh_page_num:"+xkh_page_num+"=============page:"+page);
        		//alert(lock);
        		if(lock == 'open'){
        			
        			$("#"+xkh_class_id).addClass("title_active cur").siblings().removeClass();
        			$("#"+xkh_class_id).children("span").addClass("title_color").parent().siblings().children().removeClass();
        			page = 1;
        			pageNum = (xkh_page_num)*pageNum;
        			jQuery("#sid").val(xkh_page_num);
        			sid= xkh_class_id;
        			//alert("pageNum="+pageNum+"*********lock*****"+"page="+page);
        		}
        		
                $.ajax({
                	type : "post",
    				url : "getUserInfoByClassIdV2.html",
    				dataType : "json",
    				async : false,
    				data : {currentPage:page,pageNum:pageNum,sid:sid,fid:fid2,orderPrice:orderp,maxPrice:max,minPrice:min,cityName:cname,schoolLabel:schoolLabel,serviceType:serviceType},
                    success: function(data){
                        var result = '';
//                        if(pageStart <= data.skills.length){
                            for(var j = 0; j < data.skills.length; j++){
                                result +='<li class="class_show">' +
									'<div class="class_bg"></div>' +
									'<a href="javascript:getSkillInfo('+data.skills[j].id+')">'+
									'<div class="pic_show">' +
									'<div class="class_pic">' +
									'<img src="'+data.skills[j].images[0].imgUrl+'?'+classSon_image+'" class="search_pic"/>' +
									'<div class="school_name">' +
									'  <span>' + data.skills[j].schoolName + '</span>' +
									'  </div>' +
									'<div class="busniess_name">' +
									'  <span>' + data.skills[j].skillName + '</span>' +
									'  </div>' +
									'</div>' +
									'<div class="person_pic"><img style="border-radius: 100%;" src="' + data.skills[j].headImgUrl + '" class="search_pic"  /></div>' +
									'<p class="author">' + data.skills[j].userName + '</p>' +
									'<div class="service_content">' +
									'<span class="class_service">擅长服务：</span>' +
									'<div class="service"><span>' + data.skills[j].className + '</span></div>' +
									'<div class="diamond_content">'+
									' <span class="iconfont icon-zuobiao"></span>' +
									'<span class="class_city">' +  data.skills[j].cityName + '</span>' +
									'</div>'+
									'</div>' +
									'</div>' +
									'<div class="sell_content">' +
									'<div class="sell_pic"><img src="../xkh_version_2/img/class_icon.png" class="search_pic" /></div>' +
									'<div class="sell_number">' +
									'<span>[已售</span>' +
									'<span>' + 1000 + ']</span>' +
									'</div>' +
									'<div class="sell_star">' +
									'<span class="iconfont icon-xingxing"></span>' +
									'<span class="iconfont icon-xingxing"></span>' +
									'<span class="iconfont icon-xingxing"></span>' +
									'<span class="iconfont icon-xingxing"></span>' +
									'<span class="iconfont icon-xingxing"></span>' +
									'</div>' +
									'<div class="diamond_content">'+
									'<div class="diamond">' +
									'<img src="../xkh_version_2/img/icon_diamond.png" class="search_pic"  />' +
									'</div>' +
									'<div class="num"><span>' + data.skills[j].skillPrice + '</span>元/次</div>' +
									'</div>' +
									'</div>' +
									'</div>'+
									'</li>';
                                
                            }
                            if(pageNum > data.skills.length){
                                // 数据加载完
                                tab1LoadEnd = true;
                                // 锁定  ----不用锁定
                                me.lock();
                                // 无数据
                                me.noData();
//                            /    break;
                            }
                            
                          //  back();
                          //  var b= parseInt(pageNum)-parseInt(data.skills.length)
                           // alert(b==0)
                            if(data.skills.length = pageNum){
                            	$("#"+sid).val(page+1);
                            	$("#xkh_page_num").val(page);
                            	//$.cookie('xkh_page_num', page); 
                            }
                            // 为了测试，延迟1秒加载
                            setTimeout(function(){
                                $('.lists').eq(itemIndex).append(result);
                                // 每次数据加载完，必须重置
                                me.resetload();
                               // $.cookie('xkh_lock','off'); 
                            },1000);
                            
                            if(lock == 'open'){
                            	backTwo();
                    			
                    		}
                            
                            
//                        }
                    },
                    error : function(XMLHttpRequest, textStatus, errorThrown){
      		          //通常情况下textStatus和errorThrown只有其中一个包含信息
      		          alert("失败！");
      		        me.resetload();
      		       }
                });
            // 加载菜单二的数据
            }
        }
    });
    
    function goSkillInfo(id){
    	window.location.href="${path}/home/getSkillInfo.html?id="+id;
    }
    
    function serchList(sid,orderp,fid){
    	
             $.ajax({
             	type : "post",
 				url : "getUserInfoByClassIdV2.html",
 				dataType : "json",
 				async : false,
 				data : {currentPage:1,pageNum:pageNum,sid:sid,orderPrice:orderp,fid:fid},
                 success: function(data){
                     var result = '';
                         for(var j = 0; j < data.skills.length; j++){
                             result +='<li class="class_show">' +
									'<div class="class_bg"></div>' +
									'<a href="personal_details.html">'+
									'<div class="pic_show">' +
									'<div class="class_pic">' +
									'<img src="'+data.skills[j].images[0].imgUrl+'?'+classSon_image+'" class="search_pic"/>' +
									'<div class="school_name">' +
									'  <span>' + data.skills[j].schoolName + '</span>' +
									'  </div>' +
									'<div class="busniess_name">' +
									'  <span>' + data.skills[j].skillName + '</span>' +
									'  </div>' +
									'</div>' +
									'<div class="person_pic"><img style="border-radius: 100%;" src="' + data.skills[j].headImgUrl + '" class="search_pic"  /></div>' +
									'<p class="author">' + data.skills[j].userName + '</p>' +
									'<div class="service_content">' +
									'<span class="class_service">擅长服务：</span>' +
									'<div class="service"><span>' + data.skills[j].className + '</span></div>' +
									'<div class="diamond_content">'+
									' <span class="iconfont icon-zuobiao"></span>' +
									'<span class="class_city">' +  data.skills[j].cityName + '</span>' +
									'</div>' +
									'</div>' +
									'</div>' +
									'<div class="sell_content">' +
									'<div class="sell_pic"><img src="../xkh_version_2/img/class_icon.png" class="search_pic" /></div>' +
									'<div class="sell_number">' +
									'<span>[已售</span>' +
									'<span>' + 1000 + ']</span>' +
									'</div>' +
									'<div class="sell_star">' +
									'<span class="iconfont icon-xingxing"></span>' +
									'<span class="iconfont icon-xingxing"></span>' +
									'<span class="iconfont icon-xingxing"></span>' +
									'<span class="iconfont icon-xingxing"></span>' +
									'<span class="iconfont icon-xingxing"></span>' +
									'</div>' +
									'<div class="diamond_content">'+
									'<div class="diamond">' +
									'<img src="../xkh_version_2/img/icon_diamond.png" class="search_pic"  />' +
									'</div>' +
									'<div class="num"><span>' + data.skills[j].skillPrice + '</span>元/次</div>' +
									'</div>' +
									'</div>' +
									'</div>'+
									'</li>';
                             
                         }
                         
                         // 为了测试，延迟1秒加载
                         setTimeout(function(){
                        	jQuery("#maxPrice").val("");
                     		jQuery("#minPrice").val("");
                     		jQuery("#cityName").val("");
                     		jQuery("#schoolLabel").val(""); 
                     		jQuery("#serviceType").val(""); 
                        	  $('.lists').eq(itemIndex).html(result);
                             // 每次数据加载完，必须重置
//                        	  dropload.unlock();
//                              dropload.noData(false);
                              dropload.resetload();
                         },1000);
                         
                 },
                 error : function(XMLHttpRequest, textStatus, errorThrown){
   		          //通常情况下textStatus和errorThrown只有其中一个包含信息
   		          alert("失败！");
   		       }
             });
         // 加载菜单二的数据
    }
    
    
    function back(){
    	var str = window.location.href;
		str = str.substring(str.lastIndexOf("/") + 1);
		if ($.cookie(str)) {
		$("html,body").animate({ scrollTop: $.cookie(str) }, 1000);
		  
		}
		else {
		}
		$(window).scroll(function () {
		var str = window.location.href;
		str = str.substring(str.lastIndexOf("/") + 1);
		var top = $(document).scrollTop();
		$.cookie(str,top, { path: '/' });
		return $.cookie(str);
		
		})						
	}
    function backTwo(){
    	$.cookie('xkh_lock','off'); 
    	var str = window.location.href;
		str = str.substring(str.lastIndexOf("/") + 1);
		if ($.cookie(str)) {
		$("html,body").animate({ scrollTop: $.cookie(str) }, 1000);
		  
		}
		else {
		}
		$(window).scroll(function () {
		var str = window.location.href;
		str = str.substring(str.lastIndexOf("/") + 1);
		var top = $(document).scrollTop();
		$.cookie(str,top, { path: '/' });
		return $.cookie(str);
		
		})	
    	
		
    }

	//点击排序出来的那一部分
	$(".choose_sort").click(function(){
		$(".class_sortContent").toggleClass("show");
		if($('.icon_sort').hasClass('icon-triangle')){
			$('.icon_sort').removeClass("icon-triangle").addClass("icon-triangle-copy")
		}else{
			$('.icon_sort').removeClass("icon-triangle-copy").addClass("icon-triangle")
		}
	})
	
	//点击筛选出来的
	$('.choose2').on('click', function(e){
		var wh = $('.wraper').height();
//		console.log(wh)
		$('.mask').css('height', wh).show();
//		$('.class_filterContent').css('height', wh).addClass('moved');
		
		$('.class_filterContent').addClass('moved');
	});
	
	$('.choose1').on('click', function(e){
		window.location.href="../prompt/prompt.html"
	});
	$('.mask').on('click', function(){
		$('.mask').hide();
		$('.class_filterContent').removeClass('moved');
	});
	
	//点击右侧标签部分
	$(".label_content a").click(function(){
		$(this).css({"border":"1px solid #1d9243","border-radius":"5px"});
		var index=$(this).index();
		$(".green_cha").eq(index).css("display","block")
	});
	$(".service_style div").click(function(){
		$(this).addClass("line_style").siblings().removeClass("line_style")
		var serviceStyle = this.dataset.name;
		$("#serviceType").val(serviceStyle);
		
	})
	
	function getBy(){
		var max = jQuery("#maxPrice").val();
		var min = jQuery("#minPrice").val();
		var cname = jQuery("#cityName").val();
		var schoolLabel = jQuery("#schoolLabel").val(); 
		var serviceType = jQuery("#serviceType").val(); 
	}
	
	//筛选
	$(".enure_box").click(function(){
	//	alert("进入筛选");
		$(".class_filterContent").removeClass("moved");
		$(".mask").hide();
		var max = jQuery("#maxPrice").val();
		var min = jQuery("#minPrice").val();
		var cname = jQuery("#cityName").val();
		var schoolLabel = jQuery("#schoolLabel").val(); 
		var serviceType = jQuery("#serviceType").val(); 
		
		var fid2 =  $("#fid2").val();
		$("#"+sid).val(2);
		//alert("sid:"+sid+"==="+"orderp:"+dataname+"======"+"fid:"+fid2);
		
		screen(sid,dataname,fid2,max,min,cname,schoolLabel,serviceType);
		//alert(sid+"====================page:"+page);
		
	})
//	點擊重置
	$(".reset_box").click(function(){
		
		
		jQuery("#maxPrice").val("");
 		jQuery("#minPrice").val("");
 		jQuery("#cityName").val("");
 		jQuery("#schoolLabel").val(""); 
 		jQuery("#serviceType").val(""); 
		
	
		$("#minPrice").val("");
		$("#maxPrice").val("");
		$(".show-result").css("display","none");
		$(".result-tips").css("display","block");
		$(".service_style div").removeClass("line_style");
	})
	
	function screen(sid,orderp,fid,max,min,cname,schoolLabel,serviceType){
		$.ajax({
         	type : "post",
				url : "getUserInfoByClassIdV2.html",
				dataType : "json",
				async : false,
				data : {currentPage:1,pageNum:pageNum,sid:sid,orderPrice:orderp,fid:fid,maxPrice:max,minPrice:min,cityName:cname,schoolLabel:schoolLabel,serviceType:serviceType},
             success: function(data){
                 var result = '';
                     for(var j = 0; j < data.skills.length; j++){
                         result +='<li class="class_show">' +
								'<div class="class_bg"></div>' +
								'<a href="personal_details.html">'+
								'<div class="pic_show">' +
								'<div class="class_pic">' +
								'<img src="'+data.skills[j].images[0].imgUrl+'?'+classSon_image+'" class="search_pic"/>' +
								'<div class="school_name">' +
								'  <span>' + data.skills[j].schoolName + '</span>' +
								'  </div>' +
								'<div class="busniess_name">' +
								'  <span>' + data.skills[j].skillName + '</span>' +
								'  </div>' +
								'</div>' +
								'<div class="person_pic"><img style="border-radius: 100%;" src="' + data.skills[j].headImgUrl + '" class="search_pic"  /></div>' +
								'<p class="author">' + data.skills[j].userName + '</p>' +
								'<div class="service_content">' +
								'<span class="class_service">擅长服务：</span>' +
								'<div class="service"><span>' + data.skills[j].className + '</span></div>' +
								' <span class="iconfont icon-zuobiao"></span>' +
								'<span class="class_city">' +  data.skills[j].cityName + '</span>' +
								'</div>' +
								'</div>' +
								'<div class="sell_content">' +
								'<div class="sell_pic"><img src="../xkh_version_2/img/class_icon.png" class="search_pic" /></div>' +
								'<div class="sell_number">' +
								'<span>[已售</span>' +
								'<span>' + 1000 + ']</span>' +
								'</div>' +
								'<div class="sell_star">' +
								'<span class="iconfont icon-xingxing"></span>' +
								'<span class="iconfont icon-xingxing"></span>' +
								'<span class="iconfont icon-xingxing"></span>' +
								'<span class="iconfont icon-xingxing"></span>' +
								'<span class="iconfont icon-xingxing"></span>' +
								'</div>' +
								'<div class="diamond">' +
								'<img src="../xkh_version_2/img/icon_diamond.png" class="search_pic"  />' +
								'</div>' +
								'<div class="num"><span>' + data.skills[j].skillPrice + '</span>元/次</div>' +
								'</div>' +
								'</div>'+
								'</li>';
                         
                     }
                     // 为了测试，延迟1秒加载
                     setTimeout(function(){
                    	  $('.lists').eq(itemIndex).html(result);
                         // 每次数据加载完，必须重置
//                    	  dropload.unlock();
//                          dropload.noData(false);
//                          dropload.resetload();
                    	  dropload.resetload();
                     },1000);
                     
             },
             error : function(XMLHttpRequest, textStatus, errorThrown){
		          //通常情况下textStatus和errorThrown只有其中一个包含信息
		          alert("失败！");
		       }
         });
	}

	 //选择所在地
	mui.init();
			var cityPicker = new mui.PopPicker({
				layer: 2
			});
			cityPicker.setData(citys);
			var showCityPickerButton = document.getElementById('chooseCity');
			showCityPickerButton.addEventListener('tap', function(event) {
				
				if($("div").hasClass("mui-active")){
				}else{
					cityPicker.show(function(items) {
						document.querySelector('#chooseCity .result-tips').style.display = "none";
						document.querySelector('#chooseCity .show-result').style.display = "block";
						document.querySelector('#chooseCity .show-result').innerText = items[1].text;
						$("#cityName").val(items[1].text);
						//返回 false 可以阻止选择框的关闭
						//return false;
					});
				}
				
				
				
				
			}, false);
			
			
			mui.init();
			//选择学校类型
				var schoolpicker = new mui.PopPicker({
						});
				schoolpicker.setData(las);  
			var showCityPickerButton = document.getElementById('chooseSchool');
						showCityPickerButton.addEventListener('tap', function(event) {
							if($("div").hasClass("mui-active")){
							}else{
								schoolpicker.show(function(selectItems) {
									document.querySelector('#chooseSchool .result-tips').style.display = "none";
									document.querySelector('#chooseSchool .show-result').style.display = "block";
									document.querySelector('#chooseSchool .show-result').innerText = selectItems[0].text;		
									$("#schoolLabel").val(selectItems[0].value);
								});
							}
							
						
						}, false);
					

})


